package com.yan.test7.domain;

import com.yan.test7.inter.Selector;
import com.yan.test7.inter.Touchable;

import java.util.Arrays;

public class MyArrayList implements Touchable {
    private Object[] all = new Object[2]; // 保存对象， 初始化长度为2
    private int total; // 实际存储的对象个数

    // 添加集合元素
    public void add(Object element){
        if(total>=all.length){
            all = Arrays.copyOf(all, all.length*2);
        }
        all[total++] = element;
    }
    // 删除集合元素
    public void remove(int index){
        if(index < 0 || index >= total){
            System.out.println("没有这个元素");
        }
        System.arraycopy(all, index+1, all, index, total-index-1);
        all[--total]=null;
    }
    // 查找集合元素
    public void set(int index , Object value){
        if(index < 0 || index >= total){
            System.out.println("没有这个元素");
        }
      all[index] = value;
    }
    // 查找索引
    public Object get(int index){
        if(index < 0 || index >= total){
            System.out.println("没有这个元素");
            return null;
        }
       return all[index];
    }


    @Override
    public Selector select() {

        return new Myselector();
    }

    public class Myselector implements Selector{
        public int cursor = 0;
        @Override
        public boolean hasNext() {
            return cursor != total;
        }

        @Override
        public Object next() {
            return all[cursor++];
        }
    }

}
